﻿using System;
using System.Collections.Generic;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    static class ContinueRecordHelper
    {
        public static IEnumerable<ContinueRecord> GetRecords ( byte [] bytes)
        {
            return Enumerable.Cast<ContinueRecord, BiffRecord>(GetRecords((i, b) => new ContinueRecord() {Buffer = b}, bytes));   
        }
        public static IEnumerable<BiffRecord> GetRecords (Func<int , byte [], BiffRecord>firstRecordFactory,  byte [] bytes)
        {
            List<byte[]> brokenBytes = new List<byte[]>();
            for (int i = 0; i < bytes.Length; i += 8000)
            {
                int length = bytes.Length - i;
                if (length > 8000)
                    length = 8000;
                byte[] item = new byte[length];
                Array.Copy(bytes, i, item, 0, item.Length);
                brokenBytes.Add(item);
            }
            yield return firstRecordFactory(bytes.Length, brokenBytes[0]);
            foreach (var item in Enumerable.Skip(brokenBytes, 1))
                yield return new ContinueRecord() { Buffer = item };

        }
    }
}